<template>
  <div class="navigation-widget" :style="{height: height + 'px'}" ref="navigation-widget">
    <el-carousel
      v-if="!isScroll && list.length !== 0"
      arrow="never"
      indicator-position="none"
      :loop="true"
      :height="height + ''"
      :interval="3000"
      :direction="direction"
    >
      <el-carousel-item v-for="(data, key) of list" :key="key">
        <div class="navigation-content">
          <div class="icon" v-for="(item, index) of data" :key="index">
            <div class="icon-img-div">
              <img class="icon-img"
                :src="item.url || ''"
                :alt="item.name"
                :title="item.name"
                v-if="item.url"
              />
              <div class="no-image" v-else>
                <i class="el-icon-picture" />
              </div>
            </div>
            <p class="icon-desc">{{item.name || index}}</p>
          </div>
        </div>
      </el-carousel-item>
    </el-carousel>
    <ul class="navigation-ul-scroll-x" v-else>
      <li class="navigation-li-item" v-for="(item, index) of list" :key="index">
        <div class="icon">
          <div class="icon-img-div">
            <img class="icon-img"
              :src="item.url || ''"
              :alt="item.name"
              :title="item.name"
              v-if="item.url"
            />
            <div class="no-image" v-else>
              <i class="el-icon-picture" />
            </div>
          </div>
          <p class="icon-desc">{{item.name || index}}</p>
        </div>
      </li>
    </ul>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { STORE_NAMESPACE } from '../../../../config'
export default {
  name: 'PreviewNavigation',
  props: {
    rowHeight: {
      type: [String, Number],
      default: '140',
      validator: val => !isNaN(val)
    },
    index: {
      type: Number,
      default: -1
    },
    params: {
      type: Array,
      default: () => ([])
    },
    direction: {
      type: String,
      default: 'horizontal',
      validator: val => ['horizontal', 'vertical'].includes(val)
    },
    type: {
      type: String,
      default: 'page',
      validator: val => ['page', 'scroll'].includes(val)
    },
    column: { // scroll模式列数无意义
      type: [String, Number],
      default: '4',
      validator: val => !isNaN(val)
    },
    row: { // scroll模式只支持1行
      type: [String, Number],
      default: '2',
      validator: val => !isNaN(val)
    },
    isShowImg: {
      type: Boolean,
      default: true
    },
    isShowText: {
      type: Boolean,
      default: true
    },
    textHeight: {
      type: [String, Number],
      default: '20',
      validator: val => !isNaN(val)
    },
    scale: {
      type: [String, Number],
      default: '0.75',
      validator: val => !isNaN(val)
    }
  },
  data () {
    return {}
  },
  computed: {
    isScroll () {
      return this.type === 'scroll'
    },
    x () {
      return this.column * 1
    },
    y () {
      return this.isScroll ? 1 : this.row * 1 // 滚动模式下只支持1行
    },
    list () {
      if (this.isScroll) return this.params
      const pages = []
      this.params.forEach((item, index) => {
        let page = Math.floor(index / (this.x * this.y))
        if (!pages[page]) {
          pages[page] = []
        }
        pages[page].push(item)
      })
      return pages
    },
    height () { // 容器高
      return Number(this.rowHeight) * this.y
    },
    newRowHeight () { // 新行高, 指图片高度,如果不显示文字, 行高即为图片高
      return Number(this.rowHeight) // this.isShowText ? Number(this.rowHeight) : Number(this.rowHeight) - Number(this.textHeight)
    },
    newTextHeight () { // 文本高
      return this.isShowText ? Number(this.textHeight) : 0
    },
    newImgHeight () { // 图片高
      return this.isShowImg ? (this.newRowHeight - this.newTextHeight) : 0
    }
  },
  watch: {
    'x': {
      handler (val) {
        this.$nextTick(() => {
          this.resetWidth()
        })
      },
      immediate: true
    },
    'y': {
      handler (val) {
        this.$nextTick(() => {
          this.resetWidth()
        })
      },
      immediate: true
    },
    'isShowText': {
      handler (val) {
        this.$nextTick(() => {
          this.resetWidth()
        })
      },
      immediate: true
    },
    'isShowImg': {
      handler (val) {
        this.$nextTick(() => {
          this.resetWidth()
        })
      },
      immediate: true
    },
  },
  methods: {
    resetWidth () {
      if (this.$el) {
        const el = this.$el
        if (this.isScroll) {
          el.style.setProperty('--width', this.newRowHeight + 'px')
        } else {
          el.style.setProperty('--width', `calc(100% / ${this.x})`)
        }
        el.style.setProperty('--scale', this.scale)
        el.style.setProperty('--height', `${Number(this.newRowHeight)}px`)
        el.style.setProperty('--text-height', this.newTextHeight + 'px')
      }
    }
  }
}
</script>

<style lang="less" scoped>
.navigation-widget {
  --width: 25%;
  --height: 80px; // 行高
  --text-height: 20px; // 文本高
  --scale: .75;
  width: 100%;
  height: 80px; // 容器高
  box-sizing: border-box;
  overflow: hidden;
  /**
  导航图标大小, 单行
  文字: 2-4个, 55*20/14px
  行高: 80
  宽度: 100%/列数
  图片为宽和高最小值缩放.75倍
   */
  .navigation-content {
    width: 100%;
    height: 100%;
    position: relative;
    overflow: hidden;
    color: #333;
    &:after {
      clear: both;
    }
    .icon {
      position: relative;
      overflow: hidden;
      width: var(--width);
      height: var(--height);
      float: left;
      .icon-img-div {
        overflow: hidden;
        width: 100%;
        height: calc(100% - var(--text-height));
        line-height: 1;
        box-sizing: border-box;
        margin: 0 auto;
        // padding: 8px; // 使用了scale, 就不用padding了
        .icon-img {
          display: block;
          max-width: 100%;
          max-height: 100%;
          margin: 0 auto;
          transform: scale(var(--scale));
        }
        .no-image {
          display: flex;
          justify-content: center;
          align-items: center;
          width: 100%;
          height: 100%;
          font-size: 2em;
          text-align: center;
        }
      }
      .icon-desc {
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
        display: block;
        font-size: 14px;
        // width: 55px;
        height: var(--text-height);
        // line-height: 1;
        letter-spacing: -0.41px;
        text-align: center;
        margin: 0 auto;
        word-break: keep-all;
      }
    }
  }
  .navigation-ul-scroll-x {
    position: relative;
    width: 100%;
    height: 100%;
    overflow-x: scroll;
    overflow-y: hidden;
    white-space: nowrap;
    &::-webkit-scrollbar {
      width: 0;
      height: 0;
    }
    .navigation-li-item {
      display: inline-block;
      height: 100%;
      margin-left: 5px;
      &:first-child {
        margin-left: 0;
      }
      .icon {
        position: relative;
        overflow: hidden;
        width: var(--width);
        height: var(--height);
        .icon-img-div {
          overflow: hidden;
          width: 100%;
          height: calc(100% - var(--text-height));
          line-height: 1;
          box-sizing: border-box;
          margin: 0 auto;
          // padding: 8px; // 使用了scale, 就不用padding了
          .icon-img {
            display: block;
            max-width: 100%;
            max-height: 100%;
            margin: 0 auto;
            transform: scale(var(--scale));
          }
          .no-image {
            display: flex;
            justify-content: center;
            align-items: center;
            width: 100%;
            height: 100%;
            font-size: 2em;
            text-align: center;
          }
        }
        .icon-desc {
          overflow: hidden;
          white-space: nowrap;
          text-overflow: ellipsis;
          display: block;
          font-size: 12px;
          // width: 55px;
          height: var(--text-height);
          // line-height: 1;
          letter-spacing: -0.41px;
          text-align: center;
          margin: 0 auto;
          word-break: keep-all;
        }
      }
    }
  }
}
</style>